package com.kedll.library.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


/**
 * SharedPreferences 辅助类
 *
 * @author Administrator
 */
public class StoreShareValue {
    /**
     * 用户是否登陆名字
     */
    public final static String IS_LOGIN = "IS_LOGIN";
    /**
     * 用户的key
     */
    public final static String USER = "USER";
    /**
     * 保存用户登陆状态信息的名字
     */
    public final static String USER_LOGIN_INFO = "USER_LOGIN_INFO";
    public static final String LOGIN_FLAG = "LOGIN_FLAG";

    public static final String SAVE_FILE_NAME = "MUSEUM_SHARE";

    public static final String SAVE_KEY_TOKEN = "TOKEN";

    public static final String SAVE_KEY_EXPIRES = "EXPIRES";

    public static final String SAVE_KEY_USER_ID = "USER_ID";

    public static final String SAVE_KEY_TUISONG = "TUI_SONG";

    public static final String SAVE_KEY_DEVICE_ID = "DEVICE_ID";

    public static final String SAVE_KEY_LEADER = "LEADER";

    public static final String SAVE_KEY_UPDATE = "UPDATE";

    public static final String SAVE_KEY_ADDRESSINDEX = "ADDRESSINDEX";

    public static final String SAVE_KEY_LASTID = "LASTID";

    public static final String SAVE_KEY_ADDRESS = "ADDRESS";

    public static final String SAVE_KEY_LATITUDE = "LATITUDE";

    public static final String SAVE_KEY_LONGTITUDE = "LONGTITUDE";
    public static final String SAVE_KEY_WALLPAPER = "WALLPAPER";
    public static final String SAVE_KEY_CONFIG = "CONFIG";

    private static SharedPreferences sp;

    private static SharedPreferences getDataShared(Context context, String sharedName) {
        if (sp == null) {
            sp = context.getSharedPreferences(sharedName, Context.MODE_PRIVATE);
        }
        return sp;
    }

    /**
     * 从SP文件中获取String
     *
     * @param key
     * @param defaultStr
     * @param context
     * @param sharedName
     * @return
     */
    public static String getString(String key, String defaultStr, Context context,
                                   String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        return sharedPreferences.getString(key, defaultStr);
    }

    /**
     * 把String保存到SP文件中
     *
     * @param key
     * @param value
     * @param context
     * @param sharedName
     */
    public static void putString(String key, String value, Context context,
                                 String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        sharedPreferences.edit().putString(key, value).commit();
    }

    /**
     * 从SP中获取int值
     *
     * @param key
     * @param defaultValue
     * @param context
     * @param sharedName
     * @return
     */
    public static int getInt(String key, int defaultValue, Context context,
                             String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        return sharedPreferences.getInt(key, defaultValue);
    }

    /**
     * 把int值保存到SP
     *
     * @param key
     * @param value
     * @param context
     * @param sharedName
     */
    public static void putInt(String key, int value, Context context,
                              String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        sharedPreferences.edit().putInt(key, value).commit();
    }

    /**
     * 把Boolean值保存到SP
     *
     * @param key
     * @param value
     * @param context
     * @param sharedName
     */
    public static void putBoolean(String key, Boolean value, Context context,
                                  String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        sharedPreferences.edit().putBoolean(key, value).commit();
    }

    /**
     * 从SP中获取Boolean值
     *
     * @param key
     * @param defaultValue
     * @param context
     * @param sharedName
     * @return
     */
    public static boolean getBoolean(String key, Boolean defaultValue, Context context,
                                     String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    /**
     * 保存float值到SP
     *
     * @param key
     * @param value
     * @param context
     * @param sharedName
     */
    public static void putFloat(String key, Float value, Context context,
                                String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        sharedPreferences.edit().putFloat(key, value).commit();
    }

    /**
     * 从SP中获取float
     *
     * @param key
     * @param defaultValue
     * @param context
     * @param sharedName
     * @return
     */
    public static float getFloat(String key, Float defaultValue, Context context,
                                 String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        return sharedPreferences.getFloat(key, defaultValue);
    }

    /**
     * 保存long值到SP
     *
     * @param key
     * @param value
     * @param context
     * @param sharedName
     */
    public static void putLong(String key, Long value, Context context,
                               String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        sharedPreferences.edit().putLong(key, value).commit();
    }

    /**
     * 从SP中获取long值
     *
     * @param key
     * @param defaultValue
     * @param context
     * @param sharedName
     * @return
     */
    public static long getLong(String key, Long defaultValue, Context context,
                               String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        return sharedPreferences.getLong(key, defaultValue);
    }

    private static String getSerialNumber() {
        StringBuffer serialNumber = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 20; i++) {
            int v = Math.abs(random.nextInt()) % 10;
            serialNumber.append(v);
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String time = sdf.format(new Date());
            serialNumber.append(time);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return serialNumber.toString();
    }

    /**
     * 保存对象到SP
     *
     * @param objectKeyName
     * @param object
     * @param context
     * @param sharedName
     */
    public static void putObject(String objectKeyName, Object object, Context context,
                                 String sharedName) {
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences(sharedName,
                    Context.MODE_PRIVATE);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = null;

            oos = new ObjectOutputStream(baos);

            oos.writeObject(object);
            // 将对象放到OutputStream中
            // 将对象转换成byte数组，并将其进行base64编码
            String data = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            SharedPreferences.Editor editor = sharedPreferences.edit();
            // 将编码后的字符串写到base64.xml文件中
            editor.putString(objectKeyName, data);
            editor.commit();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 用SharedPreferences从本地读取你保存的对象
     *
     * @param objectKeyName
     * @param sharedName
     * @param context
     * @return
     */
    public static Object getObject(String objectKeyName, String sharedName, Context context) {
        Object object = null;
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {

            SharedPreferences sharedPreferences = context.getSharedPreferences(sharedName,
                    Context.MODE_PRIVATE);
            String data = sharedPreferences.getString(objectKeyName, "");
            if (TextUtils.isEmpty(data))
                return null;
            // 对Base64格式的字符串进行解码
            byte[] base64Bytes = Base64.decode(data.getBytes(), Base64.DEFAULT);
            bais = new ByteArrayInputStream(base64Bytes);


            ois = new ObjectInputStream(bais);
            object = ois.readObject();
            return object;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return object;
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (bais != null) {
                try {
                    bais.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 从SP中移除某个KEY指向的数据
     *
     * @param key
     * @param context
     * @param sharedName
     */
    public static void removeKey(String key, Context context, String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        sharedPreferences.edit().remove(key).commit();
    }


    /**
     * 清除SP内容
     *
     * @param context
     * @param sharedName
     */
    public static void removeAll(Context context, String sharedName) {
        SharedPreferences sharedPreferences = getDataShared(context, sharedName);
        sharedPreferences.edit().clear().commit();
    }
}
